package fun._6742.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import fun._6742.mapper.ProductMapper;
import fun._6742.mapper.UserMapper;
import fun._6742.model.dataobject.*;
import fun._6742.model.valueobject.ProductVO;
import fun._6742.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Api(tags = "统计")
@RestController
@RequestMapping("api/admin/statistic/v1")
public class AdminStatisticController {

    @Resource
    AdminStatisticService adminStatisticService;
    @Resource
    UserMapper userMapper;
    @Resource
    AdminProductService adminProductService;
    @Resource
    ProductMapper productMapper;
    @Resource
    AdminOrderService adminOrderService;
    @Resource
    AdminCategoryService adminCategoryService;
    @Resource
    ProductOrderItemService productOrderItemService;

    @ApiOperation(value = "商品分类统计")
    @GetMapping("category")
//    类别名，数量
    public Map<String, Integer> statisticCategory() {
//        产品id-产品类别id
        Map<Long, Long> map = adminProductService.page(new Page<ProductDO>(1, 9999)).getRecords()
                .stream().collect(Collectors.toMap(ProductDO::getId, ProductDO::getCategory));
//        订单列表
        List<Long> orders = adminOrderService.list(new QueryWrapper<ProductOrderDO>().in("del", 0, 1))
                .stream().map(ProductOrderDO::getId).collect(Collectors.toList());
//        子订单列表，有产品id
        List<ProductOrderItemDO> productOrderItemDOList = productOrderItemService.list(new QueryWrapper<ProductOrderItemDO>().in("product_order_id", orders));
//        类别id-类别名
        Map<Long, String> map1 = adminCategoryService.listByIds(map.values()).stream().collect(Collectors.toMap(CategoryDO::getId, CategoryDO::getName));

        Map<String, Integer> result = new HashMap<>();
        for (ProductOrderItemDO productOrderItemDO : productOrderItemDOList) {
            result.put(map1.get(map.get(productOrderItemDO.getProductId())),
                    result.getOrDefault(map1.get(map.get(productOrderItemDO.getProductId())), 0) + 1);
        }
        return result;
    }

    @ApiOperation(value = "商品单项统计")
    @GetMapping("items")
//    产品名，数量
    public Map<String, Integer> statisticItems() {
//        订单列表
        List<Long> orders = adminOrderService.list(new QueryWrapper<ProductOrderDO>().in("del", 0, 1))
                .stream().map(ProductOrderDO::getId).collect(Collectors.toList());
//        子订单列表，有产品id
        List<ProductOrderItemDO> productOrderItemDOList = productOrderItemService.list(new QueryWrapper<ProductOrderItemDO>().in("product_order_id", orders));

        Map<String, Integer> result = new HashMap<>();
        for (ProductOrderItemDO productOrderItemDO : productOrderItemDOList) {
            result.put(productOrderItemDO.getProductName(),
                    result.getOrDefault(productOrderItemDO.getProductName(), 0) + 1);
        }
        return result;
    }

    @ApiOperation(value = "商品销量倒叙统计")
    @GetMapping("sales")
    public Map<ProductVO,Integer> statisticItemsSales() {
//        订单列表
        List<Long> orders = adminOrderService.list(null)
                .stream().map(ProductOrderDO::getId).collect(Collectors.toList());
//        子订单列表，有产品id
        List<ProductOrderItemDO> productOrderItemDOList = productOrderItemService.list(new QueryWrapper<ProductOrderItemDO>().in("product_order_id", orders));

        Map<ProductVO,Integer> result = new HashMap<>();
        Map<Long, Integer> map = new HashMap<>();
        for (ProductOrderItemDO productOrderItemDO : productOrderItemDOList) {
            map.put(productOrderItemDO.getProductId(),
                    map.getOrDefault(productOrderItemDO.getProductId(), 0) + productOrderItemDO.getBuyNum());
        }
        productMapper.selectList(null).forEach(productDO -> {
            result.put(new ProductVO(productDO), map.getOrDefault(productDO.getId(), 0));
        });
        return result;
    }

    @ApiOperation(value = "用户消费金额统计")
    @GetMapping("Consumption")
    public Map<UserDO,BigDecimal> statisticConsumption() {
//        订单列表
        List<ProductOrderDO> orders = adminOrderService.list(new QueryWrapper<ProductOrderDO>().in("del", 0, 1));
        List<UserDO> userDOList = userMapper.selectList(new QueryWrapper<UserDO>().eq("user_type", 0));
        Map<Long, BigDecimal> userIdConsumption = userDOList.stream().collect(Collectors.toMap(UserDO::getId, obj -> new BigDecimal(0)));
        orders.forEach(obj->userIdConsumption.put(obj.getUserId(), obj.getTotalAmount()));
        return userDOList.stream().collect(Collectors.toMap(obj->obj, obj -> userIdConsumption.get(obj.getId())));
    }
}
